package com.ants.modules.system.controller;

import com.ants.common.annotation.AutoLog;
import com.ants.common.system.result.Result;
import com.ants.modules.system.entity.AntsSystemConfig;
import com.ants.modules.system.entity.WxConfig;
import com.ants.modules.system.service.AntsSystemConfigService;
import com.ants.modules.system.service.WxConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * TODO
 * Author Chen
 * Date   2021/9/7 18:44
 */

@Slf4j
@Api(tags = "系统配置")
@RestController
@RequestMapping("/antsSystemConfig")
public class AntsSystemConfigController {

    @Autowired
    AntsSystemConfigService antsSystemConfigService;

    @AutoLog(value = "系统配置-列表")
    @ApiOperation(value = "系统配置-列表", notes = "系统配置-列表")
    @GetMapping("/list")
    public Result<?> queryPageList() {
        List<AntsSystemConfig> list = antsSystemConfigService.list();
        AntsSystemConfig antsSystemConfig = null;
        if (list.isEmpty()) {
            antsSystemConfig = new AntsSystemConfig();
        } else {
            antsSystemConfig = list.get(0);
        }
        return Result.ok(antsSystemConfig);
    }

    /**
     * 添加
     *
     * @param antsSystemConfig
     * @return
     */
    @AutoLog(value = "系统配置-添加")
    @ApiOperation(value = "系统配置-添加", notes = "系统配置-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AntsSystemConfig antsSystemConfig) {
        antsSystemConfigService.save(antsSystemConfig);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param antsSystemConfig
     * @return
     */
    @AutoLog(value = "系统配置-编辑")
    @ApiOperation(value = "系统配置-编辑", notes = "系统配置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AntsSystemConfig antsSystemConfig) {
        boolean ok = antsSystemConfigService.updateById(antsSystemConfig);
        if (ok) {
            return Result.ok("操作成功!");
        }
        return Result.error("操作失败!");
    }

    /**
     * @param id
     * @return
     * @功能：删除
     */
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        boolean ok = antsSystemConfigService.removeById(id);
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        boolean ok = antsSystemConfigService.removeByIds(Arrays.asList(ids.split(",")));
        if (ok) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }


}
